package com.wanczy.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wanczy.pojo.UserTable;
import com.wanczy.service.UserLoginService;
import com.wanczy.service.impl.UserLoginServiceImpl;
import com.wanczy.util.StringToDate;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
	
@WebServlet("/servlet/addUser")
public class AddUserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
		Date userBirthday = null;
		try {
			PrintWriter out = response.getWriter();
			
			String userAccount = request.getParameter("userAccount");
			int userSex = Integer.parseInt(request.getParameter("userSex"));
			String userEmail = request.getParameter("userEmail");
			int userAge = Integer.parseInt(request.getParameter("userAge"));
			String userPass = request.getParameter("userPass");
			userBirthday = StringToDate.getDate(request.getParameter("userBirthday"));
			UserTable userTable = new UserTable(0,userAccount,userSex,userEmail,userAge,userPass,1,userBirthday);
			UserLoginService service  = new UserLoginServiceImpl();
			
			boolean flag = service.doIns(userTable);
			JSONArray jsonA = new JSONArray();
			JSONObject jsono = new JSONObject();
			jsono.put("flag", flag);
			
			jsonA.add(jsono);
			out.println(jsonA.toString());
			out.close();			
		} catch (Exception e) {
			e.printStackTrace();
		}		
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
